home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October: Mac OS SDK / Dev.CD Oct 97 SDK1.toast / Development Kits (Disc 1) / Apple Color OneScanner SDK / Scan Image 1.0 / Source / PICT.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-24  |  6.4 KB  |  326 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. #
  3. #        PICT.c
  4. #
  5. #        This segment handles PICT files.
  6. #
  7. #        Author(s):     Michael Marinkovich
  8. #                    Apple Developer Technical Support
  9. #                    marink@apple.com
  10. #
  11. #        Modification History: 
  12. #
  13. #            4/3/96        MWM     Initial coding                     
  14. #
  15. #        Copyright © 1992-96 Apple Computer, Inc., All Rights Reserved
  16. #
  17. #
  18. #        You may incorporate this sample code into your applications without
  19. #        restriction, though the sample code has been provided "AS IS" and the
  20. #        responsibility for its operation is 100% yours.  However, what you are
  21. #        not permitted to do is to redistribute the source as "DSC Sample Code"
  22. #        after having made changes. If you're going to re-distribute the source,
  23. #        we require that you make it clear in the source that the code was
  24. #        descended from Apple Sample Code, but that you've made changes.
  25. #
  26. *************************************************************************************/
  27.  
  28. #include <Events.h>
  29. #include <ToolUtils.h>
  30. #include <Gestalt.h>
  31. #include <OSUtils.h>
  32. #include <Palettes.h>
  33.  
  34. #include "App.h"
  35. #include "Proto.h"
  36.  
  37.  
  38. //----------------------------------------------------------------------
  39. //
  40. //    OpenPictFile - with Spec, read file, convert to GWorld & return 
  41. //                   the new world.
  42. //
  43. //----------------------------------------------------------------------
  44.  
  45. OSErr OpenPictFile(FSSpec *spec, GWorldPtr *theWorld)
  46. {
  47.     OSErr            err;
  48.     PicHandle        thePict;
  49.     
  50.  
  51.     err = ReadPICTFile(spec, &thePict);
  52.     
  53.     if (thePict != nil && err == noErr) 
  54.     {
  55.         err = PictToWorld(thePict, theWorld);
  56.         KillPicture(thePict);
  57.     }
  58.  
  59.     return err;
  60.     
  61. }
  62.  
  63.  
  64. //----------------------------------------------------------------------
  65. //
  66. //    SavePictFile - with Spec, convert windows GWorld to a PICT and  
  67. //                   save resulting PICT to the disk.
  68. //
  69. //----------------------------------------------------------------------
  70.  
  71. OSErr SavePictFile(FSSpec *spec, GWorldPtr theWorld)
  72. {
  73.     OSErr            err;
  74.     PicHandle        thePict;
  75.     
  76.     
  77.     err = WorldToPict(theWorld, &thePict);
  78.     
  79.     if (thePict != nil && err == noErr) 
  80.     {
  81.         err = WritePICTFile(spec, thePict);
  82.         KillPicture(thePict);
  83.     }
  84.  
  85.     return err;
  86.     
  87. }
  88.  
  89. //----------------------------------------------------------------------
  90. //
  91. //    ReadPICTFile - open and read disk data, returning PICT.
  92. //                
  93. //
  94. //----------------------------------------------------------------------
  95.  
  96. OSErr ReadPICTFile(FSSpec *spec, PicHandle *thePict)
  97. {
  98.     OSErr                    err;
  99.     long                    pictSize;
  100.     long                    fileSize;
  101.     short                    refNum;
  102.  
  103.     if (spec != nil) 
  104.     {
  105.         SetCursor(*GetCursor(watchCursor));            // set the cursor to a watch while busy
  106.         
  107.         if (thePict != nil)
  108.             *thePict = nil;
  109.     
  110.         err = FSpOpenDF(spec, fsRdWrShPerm, &refNum);
  111.         if ( err == noErr ) 
  112.         {
  113.             err = GetEOF(refNum, &fileSize);
  114.             if ( err == noErr ) 
  115.             {
  116.                 err = SetFPos(refNum, fsFromMark, 512);   // set position to our picture data
  117.                 if ( err == noErr ) 
  118.                 {
  119.                     pictSize = fileSize - 512;
  120.                     *thePict = (PicHandle)NewHandle(pictSize);
  121.                     err = MemError();
  122.     
  123.                     if ( err == noErr && *thePict != nil ) 
  124.                     {
  125.                         HLock((Handle)*thePict);
  126.                         err = FSRead(refNum, &pictSize, **thePict);    // read in the pict data
  127.                         HUnlock((Handle)*thePict);
  128.                     }
  129.                 }
  130.             }
  131.         
  132.             FSClose( refNum );            // close the file
  133.             
  134.             SetCursor( &qd.arrow );        // set cursor back to arrow
  135.         }    
  136.     }
  137.     else
  138.         err = paramErr;    
  139.     
  140.     
  141.     return err;
  142.  
  143. }
  144.  
  145.  
  146.  
  147. //----------------------------------------------------------------------
  148. //
  149. //    WritePICTFile - 
  150. //                
  151. //
  152. //----------------------------------------------------------------------
  153.  
  154. OSErr WritePICTFile(FSSpec *spec, PicHandle thePict)
  155. {
  156.     OSErr                    err;
  157.     long                    pictSize;
  158.     long                    fileSize;
  159.     long                    emptySize;
  160.     short                    emptyBuffer;
  161.     short                    refNum;
  162.     short                    counter;
  163.     
  164.  
  165.     if (spec != nil) 
  166.     {
  167.         if (thePict != nil)
  168.         {
  169.             HLock((Handle)thePict);
  170.             pictSize = GetHandleSize((Handle)thePict);
  171.             
  172.             emptySize = 1;
  173.             emptyBuffer = 0;
  174.             
  175.             err = FSpCreate(spec, 'JVWR', kPICTType, smSystemScript);
  176.             
  177.             if (err == noErr)
  178.                 err = FSpOpenDF(spec,fsRdWrPerm, &refNum);
  179.                 
  180.             if (err == noErr)
  181.                 err = SetFPos(refNum, fsFromStart , 0);
  182.                 
  183.             if (err == noErr)
  184.                 for (counter = 0; counter < kHeaderSize; counter++)
  185.                     err = FSWrite(refNum, &emptySize, &emptyBuffer);
  186.                     
  187.             if (err == noErr)
  188.                 err = FSWrite(refNum, &pictSize, *thePict);
  189.                 
  190.             if (err == noErr)
  191.                 err = SetEOF(refNum, pictSize + kHeaderSize);
  192.                 
  193.             if (err == noErr)        
  194.                 err = FSClose(refNum);
  195.     
  196.             HUnlock((Handle)thePict);
  197.         }
  198.         else
  199.             err = paramErr;
  200.             
  201.     }
  202.     else
  203.         err = paramErr;        
  204.  
  205.     return err;
  206.     
  207. }
  208.  
  209.  
  210. //----------------------------------------------------------------------
  211. //
  212. //    PictToWorld - convert a PICT to a GWorld
  213. //                
  214. //
  215. //----------------------------------------------------------------------
  216.  
  217. OSErr PictToWorld(PicHandle thePict, GWorldPtr *theWorld)
  218. {
  219.     OSErr            err;
  220.     CGrafPtr        oldPort;
  221.     GDHandle        oldGD;
  222.     PixMapHandle    thePix;
  223.     Rect            theRect;
  224.     short            depth = 32;
  225.     
  226.     
  227.     if (theWorld != nil && thePict != nil)
  228.     {
  229.         theRect = (**thePict).picFrame;
  230.         
  231.         *theWorld = nil;
  232.         
  233.         err = NewGWorld(theWorld, depth, &theRect, nil, nil, 0L);
  234.         
  235.         if (err == noErr && theWorld != nil)
  236.         {
  237.             thePix = GetGWorldPixMap(*theWorld);
  238.             if (LockPixels(thePix))
  239.             {
  240.                 GetGWorld(&oldPort, &oldGD);
  241.                 
  242.                 SetGWorld(*theWorld, nil);
  243.                 EraseRect(&theRect);
  244.                 
  245.                 DrawPicture(thePict, &theRect);
  246.                 
  247.                 SetGWorld(oldPort, oldGD);
  248.                 
  249.                 UnlockPixels(thePix);
  250.             }
  251.         }                
  252.     }
  253.     else
  254.         err = paramErr;    
  255.     
  256.     return err;
  257.  
  258. }
  259.  
  260.  
  261. //----------------------------------------------------------------------
  262. //
  263. //    WorldToPict - convert a GWorld to a PICT
  264. //                
  265. //
  266. //----------------------------------------------------------------------
  267.  
  268. OSErr WorldToPict(GWorldPtr theWorld, PicHandle *thePict)
  269. {
  270.     OSErr                err = noErr;
  271.     CGrafPtr            oldPort;
  272.     GDHandle            oldGD;
  273.     PixMapHandle        thePix;
  274.     PicHandle            tempPict;
  275.     Rect                theRect;
  276.     
  277.     
  278.     if (theWorld != nil && thePict != nil)
  279.     {
  280.         *thePict = nil;
  281.         theRect = theWorld->portRect;
  282.         
  283.         thePix = GetGWorldPixMap(theWorld);
  284.         if (LockPixels(thePix))
  285.         {
  286.             GetGWorld(&oldPort, &oldGD);
  287.             
  288.             SetGWorld(theWorld, nil);
  289.             
  290.             tempPict = OpenPicture(&theRect);
  291.             
  292.             CopyBits((BitMap *)*thePix, (BitMap *)*thePix,
  293.                      &theRect, &theRect, srcCopy, nil);
  294.             
  295.             ClosePicture();
  296.             
  297.             SetGWorld(oldPort, oldGD);
  298.  
  299.             err = QDError();
  300.             
  301.             if (tempPict != nil &&     err == noErr &&
  302.                 (**tempPict).picSize != 10)
  303.             {
  304.                 *thePict = tempPict;
  305.             }         
  306.             else
  307.                 tempPict = nil;
  308.                 
  309.             UnlockPixels(thePix);
  310.         }
  311.             
  312.     }
  313.     else
  314.         err = paramErr;
  315.  
  316.     return err;
  317.  
  318. }
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.